<?php
/*
 *@Author 	: İbrahim Halil SANDUVAÇ
 *@Date 	: 02.08.2011 - 
 */

include_once "mysql.class.php";
include_once "pv.class.php";

class hh extends MySQL{
	
	private $pv;
	
	/*
	 *@function __construct : constuction fn
	 *@access : public
	 *@param Bool $start_session=true
	 *@param Bool $connect_mysql=true
	 *@param Bool $select_db=true
	 *@param String $config_file="config.php"
	 */
	public function __construct($start_session = true, $connect_mysql = true, $select_db = true, $config_file="config.php"){
		
		date_default_timezone_set('Europe/Istanbul');
		
		if($start_session == true){
			session_start();	
		}
		if($connect_mysql == true){
			$this->connect($this->get_config_info($config_file,1),$this->get_config_info($config_file,2),$this->get_config_info($config_file,3));
			if($select_db)
				$this->select_db($this->get_config_info($config_file,4));
		}
		
		$this->pv = new pv();
	}
	
	######################### GENERAL FUNCTIONS #########################
	
	/*
	 *@function set_message : sets a yp message
	 *@access : public
	 *@param String $message_name
	 *@param String $message
	 */
	public function set_message($message_type, $message){
		if(!session_is_registered("hh_message"))
			session_register("hh_message");
		$_SESSION["hh_message"]["message"] = $message;
		$_SESSION["hh_message"]["type"] = $message_type;
	}
	
	/*
	 *@function get_message : gets yp message
	 *@access : public
	 *@param String $message_name
	 *@param Bool $release=false
	 *@return String message
	 */
	public function get_message($release=false){
		if(session_is_registered("hh_message"))
		{
			if($_SESSION["hh_message"] == false)
				return false;
			else{
				$res = $_SESSION["hh_message"];
				if($release == true)
					$_SESSION["hh_message"] = false;
				return $res;
			}
		}
		else
			return false;
	}
	
	/*
	 * @function curr_date : returns current date
	 * @acccess : public
	 * @return String : current date
	 */
	public function curr_date()
	{
		return date("Y-m-d H:i:s");	
	}
	
	/*
	 * @function go : redirects an adress
	 * @acccess : public
	 * @param String $adress : redirect adress
	 */
	public function go($adress)
	{
		if($adress == "back"){
		?><script type="text/javascript">history.go(-1);</script><?php
		}
		elseif($adress == "refresh"){
		?><script type="text/javascript">location.reload(true);</script><?php
		}
		else{
		?>
		<script>window.location='<?php echo $adress;?>';</script>
        <?php
		}
	}	
	
	/*
	 * @function reverse_date : reforms date which is get by current_date()
	 * @access : public
	 * @param String $date : date
	 * @param String $seperator="-" : date seperator = 10-06-2011
	 * @return String : date
	 */
	public function reverse_date($date, $seperator="-"){
		
		$slice1 = explode(" ", $date);
		$slice2 = explode("-",$slice1[0]);
		
		return	$slice2[2].$seperator.$slice2[1].$seperator.$slice2[0]." ".$slice1[1];
		
	}
	
	/*
	 * @function db_add : insert data to databese
	 * @access : public 
	 * @param String $table : table_name
	 * @param String $values : datas
	 * @return : mysql_query
	 */
	public function db_add($table, $values){
		
		return $this->query("INSERT INTO $table VALUES($values)");
		
	}
	
	/*
	 * @function db_del : delete data from databese
	 * @access : public 
	 * @param String $table : table_name
	 * @param Mixed $id : id(s)
	 * @return : mysql_query
	 */
	public function db_del($table, $id){
		
		return $this->query("DELETE FROM $table WHERE id IN($id)");
		
	}
	
	/*
	 * @function db_update : update data on databese
	 * @access : public 
	 * @param String $table : table_name
	 * @param String $update : updates
	 * @param String $where : where
	 * @return : mysql_query
	 */
	public function db_update($table, $update, $where){
		
		return $this->query("UPDATE $table SET $update WHERE $where");
		
	}
	
	/*
	 *@function db_get : get datas from database
	 *@access : public
	 *@param String $select_name : selected datas
	 *@param String $table : table
	 *@param String $where : where
	 *@param String $extras=NULL : extra
	 *@return : mysql_query
	  */
	public function db_get($select_name, $table, $where=1, $extras=NULL){
		
		return $this->query("SELECT $select_name FROM $table WHERE $where $extras");
		
	}
	
	/*
	 *@function get_config_info : get config info
	 *@access : public
	 *@param String $config_file : config file's address
	 *@param Int $order : variable order
	 *@return String : var value
	 */
	public function get_config_info($config_file, $order){
		$file = @fopen($config_file, "r");
		
		$contents = fread($file, filesize($config_file));
		
		$contents = str_replace("<?php","",str_replace("?>","",$contents));
		
		$parts = explode(";",$contents);
		
		$second_parts = explode(",",$parts[$order-1]);
		
		return str_replace(array("'",')'),array("",""),$second_parts[1]);
	}
	
	/*
	 *@function random_name : create random name for files
	 *@access : public
	 *@param String $folder : destination folder
	 *@param String $file_name : file name
	 *@return String new URL
	 */
	public function random_name($folder,$file_name){
		$slice1 = explode(".",$file_name);
		
		$ext = $slice1[count($slice1)-1];
		
		
		$date = date("YmdHis");
		$rand_val = rand(10000,99999);
		$name = $rand_val."-".$date.".".$ext;
		
			return $folder."/".$name;
				
	}
	
	/*
	 *@function escape_sql_string : clean sql special characters
	 *@access : public
	 *@param Mixed $value
	 *@return Mixed : clear value
	 */
	public function escape_sql_string($value){
		return str_replace(array("{", "}", "|", "\\", "^", "~", "[", "]", "`", "'"),"",$value);
	}
	
	######################### USER FUNCTIONS #########################
	
	/*
	 * @function user_login : user login
	 * @access : public
	 * @param String $username : username
	 * @param String $password : password
	 * @return Bool : user_loginned?
	 */
	public function	user_login($username,$password){
		
		$password = md5($password);
		
		$sql_user_login = $this->db_get("*","users","username='$username' AND password='$password' AND block=0");
		
		if($this->num_rows($sql_user_login) == false){
			return false;
		}
		else{
			session_register("current_user");	
			
			$_SESSION["current_user"] = $this->fetch_array($sql_user_login);
			
			$curr_date = $this->curr_date();
			
			$this->db_update("users","last_access_date='$curr_date'","id=".(int)$_SESSION['current_user']["id"]);
			
			return true;
		}
		
	}
	
	/*
	 * @function user_login : user login
	 * @access : public
	 * @param Int $id : user_id
	 * @return Bool : user_loginned?
	 */
	public function	user_login_just_id($id){
		
		$id = (int)$id;
		
		$sql_user_login = $this->db_get("*","users","id=$id AND block=0");
		
		if($this->num_rows($sql_user_login) == false){
			return false;
		}
		else{
			session_register("current_user");	
			
			$_SESSION["current_user"] = $this->fetch_array($sql_user_login);
			
			$curr_date = $this->curr_date();
			
			$this->db_update("users","last_access_date='$curr_date'","id=".(int)$_SESSION['current_user']["id"]);
			
			return true;
		}
		
	}
	
	/*
	 * @function user_logout : logout
	 * @access : public
	 */
	public function user_logout(){
				
		session_destroy();

	}
	
	/*
	 * @function user_login_check : checks user is loginned
	 * @access : public
	 * @return Bool 
	 */
	public function user_login_check(){
		if(session_is_registered("current_user"))	
			return true;
		else
			return false;
	}
	
	/*
	 * @function user_register : registers user
	 * @access : public
	 * @param String $username : username
	 * @param String $password : password
	 * @param String $name : name
	 * @param Int $authority : authority
	 * @param String $email : email
	 * @param Int $block : block
	 */
	public function user_register($username, $password, $name, $authority, $email, $block){
		
		$password = md5($password);
		$curr_date = $this->curr_date();
		
		$this->db_add("users", "null, '$username', '$password', '$name', $authority, '$email', 0, $block, '$curr_date', '$curr_date'");		
		
	}
	
	/* 
	 * @function del_user : delete user with/without comments/complaints
	 * @access : public
	 * @param Int $id : user id
	 * @param Bool $del_complaints : delete user's complaints when user deleted - for TRUE delete user's complaint for FALSE move user's complaints to default user
	 */
	public function del_user($id, $del_complaints, $del_comments){
		
		$id = (int)$id;
		
		$this->db_del("users",$id);
		
		if($del_complaints){
			$sql_complaints = $this->db_get("id","complaints","author=$id");
			while($fetch_complaints = $this->fetch_array($sql_complaints)){
				$this->del_complaint($fetch_complaints["id"]);					//Del complaints
			}
		}
		else{
			$default_user = $this->fetch_array( $this->db_get("value", "settings", "id=2") );	
			$default_user = (int)$default_user["value"];
			
			$this->db_update("complaints", "author=$default_user", "author=$id");	//Move complaints to default user
		}
		
		if($del_comments){
			$this->query("DELETE FROM comments WHERE author=$id");	//Del comments
		}
		else{
			$default_user = $this->fetch_array( $this->db_get("value", "settings", "id=2") );	
			$default_user = (int)$default_user["value"];
			
			$this->db_update("comments", "author=$default_user", "author=$id");	//Move comments to default user
		}
	}
	
	/*
	 * @function update_user : update user
	 * @access : public
	 * @param Int $id : user id
	 * @param String $username : username
	 * @param String $password : password
	 * @param String $name : name
	 * @param Int $authority : authority
	 * @param String $email : email
	 * @param Int $block : block
	 */
	public function update_user($id, $username, $password, $name, $authority, $email, $block){
		
		$id = (int)$id;
		
		if($password != false)
			$this->db_update("users", "username='$username', password='".md5($password)."', name='$name', authority=$authority, email='$email', block='$block'", "id=$id");
	}
	
	######################### PRIVILEGE FUNCTIONS #########################
	
	/*
	 *@function can_access_authority - check authority for access
	 *@access : public
	 *@param Int $for : privilege order
	 *@param Int $authority=true : authority id - for TRUE value it gets current user's authority
	 *@return Bool authority access
	 */
	public function can_access_authority($for, $authority=true){
		if($authority)
			$authority = (int)$_SESSION["current_user"]["authority"];
		else
			$authority = (int)$authority;
		
		$fetch_authority = $this->fetch_array($this->db_get("privileges","authorities","id=$authority"));
		
		$split_auth = str_split($fetch_authority["privileges"]);
		
		if($split_auth[$for + 1] == 1)
			return true;
		else
			return false;
	}
	
	/*
	 *@function can_access_block - check block for access
	 *@access : public
	 *@param Int $user_id=true : user id - for TRUE value it gets current user's block
	 *@return Bool block access
	 */
	public function can_access_block($user_id=true){
		if($user_id){
			if((int)$_SESSION["current_user"]["block"] == 0)	
				return true;
			else
				return false;			
		}
		else{
			$user_id = (int)$user_id;
			$fetch_user = $this->fetch_array($this->db_get("block","users","id=$user_id"));
			if((int)$fetch_user["block"] == 1)
				return false;
			else
				return true;
		}
	}
	
	/*
	 *@function can_access - check block and authority for access
	 *@access : public
	 *@param Int $for : authority order
	 *@param Int $user_id=true : user id - for TRUE value it gets current user
	 *@return Bool block access
	 */
	public function can_access($for, $user_id=true){
		if($user_id){
		if(session_is_registered("current_user")){
			if($for == "*" || $for == "all"){
				$access_auth = true;
			}
			else{
				//$access_auth = $this->can_access_authority($for);
				if((int)$_SESSION["current_user"]["authority"] == $for)	//admin=1, editor=2, user=3
					$access_auth = true;
				else
					$access_auth = false;	
			}
				
			$access_block = $this->can_access_block();
			
			if($access_auth == true && $access_block == true)
				return true;
			else
				return false;
		}
		else
			return false;
		}
		
		else{
			if($for != "*" || $for != "all"){
				$fetch_auth = $this->fetch_array($this->db_get("authority","users","id=$user_id"));	
				$authority_id = (int)$fetch_auth["authority"];
				$access_auth = $this->can_access_authority($for, $authority_id);
			}
			else
				$access_auth = true;
			$access_block = $this->can_access_block($user_id);
			if($access_auth && $access_block)
				return true;
			else
				return false;
		}
	}
	
	######################### CATEGORIES FUNCTIONS #########################
	
	/*
	 *@function add_category : adds category(3D)
	 *@access : public
	 *@param String $category_name : category name
	 *@param Int $type : category type - 1=main, 2=sub, 3=sub_sub
	 *@param Int $parent=0 : category parent id - for main = 0
	 *@return Bool
	 */
	public function add_category($category_name, $type, $parent=0){
		if($type == "main" || $type == 1)
			$type = 1;
		elseif($type == "sub" || $type == 2)
			$type = 2;
		elseif($type == "sub_sub" || $type == 3)
			$type = 3;
		
		$parent = (int)$parent;
		
		if($parent != 0 && $this->num_rows($this->query("SELECT id FROM categories WHERE id=$parent")) < 1)
			return false;	//parent not exist
		else{
			$this->db_add("categories","null, '$category_name', $type, $parent");
			return true;	//category added
		}
	}
	
	/*
	 *@function del_category : del category, del/move sub/sub-sub categories, del/move sub categories complaints
	 *@access : public
	 *@param Int $id : category id
	 *@param Bool $del_sub_categories : del sub categories - for FALSE move sub categories to default categories
	 *@param Bool $del_complaints : del complaints - for FALSE move complaints to default categories
	 */
	public function del_category($id, $del_sub_categories, $del_complaints){
		
		$category = $this->fetch_array($this->db_get("*","categories","id=$id"));
		
		if($del_complaints){
			
			if($category["type"] == 3)
				$this->query("DELETE FROM complaints WHERE category=$id");	//delete complaints	
				
			elseif($category["type"] == 2){
				$sql_sub_categories = $this->db_get("id", "categories", "parent=$id");
				while($fetch_sub_categories = $this->fetch_array($sql_sub_categories)){
					$this->query("DELETE FROM complaints WHERE category=".$fetch_sub_categories['id']);		//Delete sub complaints
				}
			}
			
			elseif($category["type"] == 1){
				$sql_sub_categories = $this->db_get("id", "categories", "parent=$id");
				while($fetch_sub_categories = $this->fetch_array($sql_sub_categories)){
					$sql_sub_sub_categories = $this->db_get("id", "categories", "parent=".$fetch_sub_categories['id']);
					while($fetch_sub_sub_categories = $this->fetch_array($sql_sub_sub_categories)){
						$this->query("DELETE FROM complaints WHERE category=".$fetch_sub_sub_categories['id']);		//Delete sub-sub complaints
					}
				}
			}
			
		}
		else{
			
			if($category["type"] == 3){
				$default_category = $this->fetch_array($this->db_get("value","settings","id=1"));
				$default_category = (int)$default_category["value"];
				
				$this->db_update("complaints", "category=$default_category", "category=$id");	//Move complaints to default category
			}
			
			elseif($category["type"] == 2){
				$default_category = $this->fetch_array($this->db_get("value","settings","id=6"));
				$default_category = (int)$default_category["value"];
				
				$sql_sub_categories = $this->db_get("id","categories","parent=$id");
				while($fetch_sub_categories = $this->fetch_array($sql_sub_categories)){
					$this->db_update("complaints", "category=$default_category", "category=".$fetch_sub_categories['id']);	//Move complaints to default sub category
				}
			}
			
			elseif($category["type"] == 1){
				$default_category = $this->fetch_array($this->db_get("value","settings","id=5"));
				$default_category = (int)$default_category["value"];
				
				$sql_sub_categories = $this->db_get("id","categories","parent=$id");
				while($fetch_sub_categories = $this->fetch_array($sql_sub_categories)){
					$sql_sub_sub_categories = $this->db_get("id","categories","parent=".$fetch_sub_categories['id']);
					while($fetch_sub_sub_categories = $this->fetch_array($sql_sub_sub_categories)){
						$this->db_update("complaints", "category=$default_category", "category=".$fetch_sub_sub_categories['id']);	//Move complaints to default main category
					}
				}
			}
				
		}
		
		if($del_sub_categories){

			if($category["type"] == 2){
				$this->query("DELETE FROM categories WHERE parent=$id");	//delete sub-category
			}
			elseif($category["type"] == 1){
				
				$sql_sub_categories = $this->db_get("id", "categories", "parent=$id");
				while($fetch_sub_categories = $this->fetch_array($sql_sub_categories)){
					$this->query("DELETE FROM categories WHERE parent=".$fetch_sub_categories['id']);	// Delete sub-sub-categories
				}
				$this->query("DELETE FROM categories WHERE parent=$id");	// and Delete sub-categories
				
			}
		}
		else{
			
			if($category["type"] == 1){
				$default_main_category = $this->fetch_array($this->db_get("value","settings","id=5"));
				$default_main_category = (int)$default_main_category["value"];
				
				$this->db_update("categories", "parent=$default_main_category", "parent=$id");	//Move sub categories to default main category
			}
			elseif($category["type"] == 2){
				$default_sub_category = $this->fetch_array($this->db_get("value","settings","id=6"));
				$default_sub_category = (int)$default_sub_category["value"];
				
				$this->db_update("categories", "parent=$default_sub_category", "parent=$id");	//Move sub-sub categories to default sub category
			}
			
		}
		
		$this->db_del("categories",$id);	//At the final delete category
	}
	
	/*
	 *@function update_category : update category
	 *@access : public
	 *@param Int $id : category id
	 *@param String $category_name
	 */
	public function update_category($id, $category_name){
		$this->db_update("categories", "category_name='$category_name'", "id=$id");
	}
	
	######################### COMMENTS FUNCTIONS #########################
	
	/*
	 *@function add_comment : add comment
	 *@access : public
	 *@param Int $author
	 *@param Int $for : complaint id
	 *@param String $content : comment content
	 */
	public function add_comment($author, $for, $content){
		
		$curr_date = $this->curr_date();
		$block = $this->fetch_array($this->db_get("value","settings","id=3"));
		$block = (int)$block["value"];
		$for = (int)$for;
		$author = (int)$author;
		
		$this->db_add("comments","null, $author, $for, '$content', '$curr_date', $block");
		$this->db_update("complaints","comment_count=comment_count+1","id=$for");
	}
	
	/* DO NOT USE THIS
	 *@function del_comment : del comment
	 *@access : public
	 *@param Int $id : comment id
	 */
	public function del_comment($id){
		
		$complaint_id = $this->fetch_array($this->db_get("for","comments","id=$id"));
		$complaint_id = $complaint_id["for"];		
		
		$this->db_del("comments",$id);
		$this->db_update("complaints","comment_count=comment_count-1","id=$complaint_id");
	}
	
	######################### COMPLAINT FUNCTIONS #########################
	
	/*
	 *@function add_complaint : add complaint
	 *@access : public
	 *@param String $title
	 *@param String $content
	 *@param Int $category : category id
	 *@param String $photo_url : photo's server url
	 *@param Int $author : author id
	 *@param String $to_complaint : complaint destination
	 */
	public function add_complaint($title, $content, $category, $photo_url, $author, $to_complaint){
		
		$block = $this->fetch_array($this->db_get("value","settings","id=4"));
		$block = (int)$block["value"];
		$curr_date = $this->curr_date();
		
		if($this->num_rows($this->db_get("id","categories","id=$category")) == false){
			$category = $this->fetch_array($this->db_get("value","settings","id=1"));
			$category = $category["value"];
		}
		if($this->num_rows($this->db_get("id","users","id=$author")) == false){
			$author = $this->fetch_array($this->db_get("value","settings","id=2"));
			$author = $author["value"];
		}
		
		$this->db_add("complaints","null, '$title', '$content', $category, '$photo_url', $author, '$to_complaint', '', $block, '$curr_date', 0, 0, '', 0, 0");
	}
	
	/*
	 *@function del_complaint : del complaint with comments
	 *@access : public
	 *@param Int $id : complaint id
	 */
	public function del_complaint($id){
		
		$this->db_del("complaints",$id);
		
		$sql_comments = $this->db_get("id","comments","for=$id");
		
		while($fetch_comments = $this->fetch_array($sql_comments)){
			$this->db_del("comments",$fetch_comments["id"]);			//Delete complaint's comments
		}
	}
	
	/*
	 *@function update_complaint : update complaint
	 *@access : public
	 *@param Int $id : complaint id
	 *@param String $title
	 *@param String $content
	 *@param Int $category : category id
	 *@param String $photo_url : photo's server url
	 *@param String $to_complaint : complaint destination
	 */
	public function update_complaint($id, $title, $content, $category, $photo_url, $to_complaint){
		
		if($this->num_rows($this->db_get("id","categories","id=$category")) == false){
			$category = $this->fetch_array($this->db_get("value","settings","id=1"));
			$category = $category["value"];
		}
		
		$this->db_update("complaints", "title='$title', content='$content', photo='$photo_url', to_complaint='$to_complaint'", "id=$id");
	}
	
	/*
	 *@function search_detailed : search
	 *@access : public
	 *@param String $search_on : search table - complaints, articles
	 *@param String $key : keywords
	 *@param String $condition : search conditions
	 *@return Resource : MySQL select query
	 */
	public function search_detailed($search_on, $key, $conditions){
		
		$key = str_replace(array("{", "}", "|", "\\", "^", "~", "[", "]", "`", "'"),"",$key);	//unsafe chars
		$search_on = str_replace(array("{", "}", "|", "\\", "^", "~", "[", "]", "`", "'"),"",$search_on);	//unsafe chars
		
		return $this->query("SELECT * FROM $search_on WHERE $conditions LIKE '%$key%'");
	}
	
	/*
	 *@function vote_check : check ip's vote.
	 *@access : public
	 *@param Int $complaint_id
	 *@param String $ip : ip adress
	 *@return Bool : for TRUE ip has been voted for FALSE ip hasn't voted yet
	 */
	public function vote_check( $complaint_id, $ip ){
		
		$ip_list = $this->fetch_array($this->db_get("vote_ip", "complaints", "id=$complaint_id"));
		$ip_list = $ip_list["vote_ip"];
		
		if(strstr($ip_list, $ip) == false)
			return false;
		else
			return true;
			
		unset($ip_list);
		
	}
	
	/*
	 *@function vote : vote complaint
	 *@access : public
	 *@param Int $complaint_id
	 *@param String $ip : ip adress
	 *@param Int $vote : vote (1-10)
	 *@return Bool : for TRUE vote is successfull for FALSE vote failed
	 */
	public function vote($complaint_id, $ip, $vote){
		
		if($this->vote_check($complaint_id, $ip) == false){
			$complaint = $this->fetch_array( $this->db_get("*","complaints","id=$complaint_id") );
			
			$new_vote = round((($complaint["vote"]*$complaint["vote_count"]) + ($vote*10)) / ($complaint["vote_count"]+1));
			
			$ip_list = $this->fetch_array($this->db_get("vote_ip", "complaints", "id=$complaint_id"));
			$ip_list = $ip_list["vote_ip"].$ip.";";
			
			$this->db_update("complaints", "vote=$new_vote, vote_ip='$ip_list', vote_count=".($complaint['vote_count']+1), "id=$complaint_id");
			
			unset($ip_list);
			
			return true;
		}
		
		else
			return false;		
		
	}
	
	/*
	 *@function read : read complaint
	 *@access : public
	 *@param Int $id
	 *@param String $read - complaint, announcement, article
	 */
	public function read($id, $read){
		if(isset($_COOKIE["hastahakki_read_".$read."_".$id]) != true){
			$read_count = $this->fetch_array($this->db_get("read_count",$read."s","id=$id"));
			$read_count = (int)$read_count["read_count"]+1;
			$this->db_update($read."s", "read_count=$read_count", "id=$id");
			
			setcookie("hastahakki_read_".$read."_".$id, true, time()+3600*12);	//12 hours
		}
	}
	
	/*
	 *@function watch_complaint : add user to complaint's watchlist
	 *@access : public
	 *@param Int $complaint_id
	 *@param Int $user_id
	 */
	public function watch_complaint($complaint_id, $user_id){
		$watchlist = $this->fetch_array($this->db_get("watchlist","complaints","id=$complaint_id"));
		$watchlist = $watchlist["watchlist"];
		
		if(empty($watchlist) || $watchlist == "" || $watchlist == NULL)	//If it is the first
			$watchlist = "-".$user_id."-";
		else	//If it is not first
			$watchlist = $watchlist.$user_id."-";
		
		$this->db_update("complaint", "watchlist='$watchlist'", "id=$complaint_id");
		
		unset($watchlist);
		
	}
	
	/*
	 *@function send_email_to_watchlist : send email to watchlist
	 *@access : public
	 *@param Int $complaint_id
	 *@param String $email_title
	 *@param String $email_content
	 */
	public function send_email_to_watchlist($complaint_id, $email_title, $email_content){
		$watchlist = $this->fetch_array($this->db_get("watchlist"));
		$watchlist = $watchlist["watchlist"];
		$watchlist = explode("-",$watchlist);
		
		for($x = 0; $x < count($watchlist); $x++){
			$watchlist[$x] = (int)$watchlist[$x];
			$watchlist[$x] = $this->fetch_array($this->db_get("email","users","id=".$watchlist[$x]));
			$watchlist[$x] = $watchlist[$x]["email"];
			mail($watchlist, $email_title, $email_content);
		}
		
	}
	
	######################### ARTICLE FUNCTIONS #########################
	
	/*
	 *@function add_article : add article
	 *@access : public
	 *@param String $title
	 *@param String $content
	 *@param Int $author
	 */
	public function add_article($title, $content, $author){
		$block = $this->fetch_array($this->db_get("value","settings","id=7"));
		$block = (int)$block["value"];
		$curr_date = $this->curr_date();
		
		$this->db_add("articles","null, '$title', '$content', $author, $block, '$date', 0");
	}
	
	/* DO NOT USE THIS
	 *@function del_article : delete article
	 *@access : public
	 *@param Int $id
	 */
	public function del_article($id){
		$this->db_del("articles",$id);
	}
	
	/*
	 *@function update_article : update article
	 *@access : public
	 *@param Int $id
	 *@param String $title
	 *@param String $content
	 */
	public function update_article($id, $title, $content){
		$this->db_update("articles", "title='$title', content='$content'", "id=$id");
	}
	
	######################### ANNOUNCEMENT FUNCTIONS #########################
	
	/*
	 *@function add_announcement : add announcement
	 *@access : public
	 *@param String $title
	 *@param String $content
	 */
	public function add_announcement($title, $content){
		$curr_date = $this->curr_date();
		
		$this->db_add("announcements","null, '$title', '$content', '$date', 0");
	}
	
	/* DO NOT USE THIS
	 *@function del_announcement : delete announcement
	 *@access : public
	 *@param Int $id
	 */
	public function del_announcement($id){
		$this->db_del("announcements",$id);
	}
	
	/*
	 *@function update_announcement : update announcement
	 *@access : public
	 *@param Int $id
	 *@param String $title
	 *@param String $content
	 */
	public function update_announcement($id, $title, $content){
		$this->db_update("announcements", "title='$title', content='$content'", "id=$id");
	}
	
	######################### OTHER FUNCTIONS #########################
	
	/*
	 *@function save_photo : resize and save photo
	 *@access : public
	 *@param String $folder
	 *@param Mixed $image : String for URL, Resource for file
	 *@param Int $width
	 *@param Int $height
	 *@return Bool : for FALSE photo saving failed, otherwise String URL
	 */
	public function save_photo( $folder, $image, $width, $height ){
		
		//
		//Upload Image
		//
		
		if(is_string($image)){		//Image from URL
		
			$extension = explode(".",$image);
			$extension = strtolower($extension[count($extension)-1]);
			
			if($extension != "" && array_search($extension,array("gif","jpg","jpeg","png","wbmp","xbm")) != false){	//check file format
				
				$image_name = explode("/",$image);
				$image_name = $image_name[count($image_name)-1];
				$image_name = $this->random_name($folder,$image_name);
			
				file_put_contents($image_name, file_get_contents($image));		//put file
				$ret = true;
			}
			else{
				$ret = false;
			}
		}
		else{	//Image from file
			$extension = explode(".",$image["name"]);
			$extension = strtolower($extension[count($extension)-1]);
			if($extension != "" && array_search($extension,array("gif","jpg","jpeg","png","wbmp","xbm")) != false){	//check file format
				
				$image_name = $this->random_name($folder, $image["name"]);
				move_uploaded_file($image["tmp_name"], $image_name);
				$ret = true;
			}
			else{
				$ret = false;
			}
			
		}
		
		//
		//Resize Image
		//									
	if($ret){
		$resize_image = $this->pv->loadImage($image_name);	//load
		$resize_image = $this->pv->resize($resize_image, $this->pv->size($width,$height));	//resize
		$this->pv->saveImage($resize_image,$image_name,70);	//save
		$this->pv->releaseImage($resize_image);		//Clean RAM
		unset($resize_image);	//Clean RAM
	}
		
		//
		//Return operation result(Bool)
		//
		
		if($ret)
			return $image_name;
		else
			return false;	//"not a file or not supported format";
	}
		
}

?>